package com.dongjiantong.web.protal.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.biz.model.FarmStock;
import com.dongjiantong.biz.service.FarmStockService;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.utils.MD5Util;
import com.dongjiantong.common.utils.RandomUtil;
import com.dongjiantong.common.utils.StringUtil;
import com.dongjiantong.common.web.sso.LoginInfo;
import com.dongjiantong.common.web.util.UserUtil;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.enums.AnimalTypeEnum;
import com.dongjiantong.sys.model.SysLabelModel;
import com.dongjiantong.sys.service.AnimalTypeService;
import com.dongjiantong.sys.service.SysAreaService;
import com.dongjiantong.user.enums.FarmsEnum;
import com.dongjiantong.user.enums.UserEnum;
import com.dongjiantong.user.model.*;
import com.dongjiantong.user.model.dtos.FarmStockDto;
import com.dongjiantong.user.service.*;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Slf4j
@OpenBean
public class FarmsController {

    @Reference(version = "1.0.0")
    public FarmsService farmsService;

    @Reference(version = "1.0.0")
    public MzUserService mzUserReadService;

    @Reference(version = "1.0.3")
    public AnimalTypeService animalTypeService;
    @Reference(version = "1.0.0")
    public FarmStockService farmStockService;
    @Reference(version = "1.0.0")
    private RoleAppUserService roleAppUserService;
    @Reference(version = "1.0.0")
    private OrgUserAreaService orgUserAreaService;
    @Reference(version = "1.0.0")
    private SysAreaService sysAreaService;
    @Reference(version = "1.0.0")
    private SSOLoginService ssoLoginService;
    @Reference(version = "1.0.0")
    private RegionAccountService regionAccountService;
    @Reference(version = "1.0.0")
    private FarmsUserService farmsUserService;

    /**
     * 查看所有养殖场列表
     *
     * @param areaList   区域（数组）
     * @param farmId     养殖场名字
     * @param linkman    联系人名字
     * @param phone      手机号
     * @param animalType 动物种类（数组）
     * @param pageNum    页码
     * @param pageSize   一页数
     * @return
     */
    @OpenMethod(
            key = "web.farms.breedList",
            paramNames = {"areaList", "farmId", "linkman", "phone", "animalType", "pageNum", "pageSize"},
            httpMethods = RequestMethod.GET
    )
    public Response<PageInfo<Farms>> farmsBreedList(List<Integer> areaList,
                                                    Long farmId,
                                                    String linkman,
                                                    String phone,
                                                    List<Integer> animalType,
                                                    Integer pageNum,
                                                    Integer pageSize) {

        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

        if (areaList.size() == 0 && StringUtil.isBlank(linkman) && StringUtil.isBlank(phone) && animalType.size() == 0) {
            LoginInfo loginInfo = UserUtil.getCurrentUser();
            PageInfo<Farms> pageInfo = farmsService.selectByUserId(pageNum, pageSize, loginInfo.getId());
            return Response.ok(pageInfo);
        }


        //基本搜索
        Farms farms = new Farms();
        farms.setFarmsType(FarmsEnum.Breed.getCode());
        Integer areaSize = areaList.size();
        switch (areaSize) {
            case 1:
                farms.setProvinceId(areaList.get(0));
                break;
            case 2:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                break;
            case 3:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                break;
            case 4:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                break;
            case 5:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                farms.setVillageId(areaList.get(4));
                break;
            default:

                break;


        }

        farms.setId(farmId);
        farms.setLinkman(linkman);
        farms.setPhone(phone);

        if (animalType != null) {
            Integer animalTypeLever = animalType.size();
            switch (animalTypeLever) {
                case 1:
                    farms.setAnimalOneType(animalType.get(0));
                    break;
                case 2:
                    farms.setAnimalTwoType(animalType.get(1));
                    break;
                case 3:
                    farms.setAnimalTwoType(animalType.get(1));
                default:
                    break;

            }
        }

        //   farms.setDeleteFlag(0); //boss可以看到所有

        PageInfo<Farms> pageInfo = farmsService.getFarmsListByPage(pageNum, pageSize, farms);


        return Response.ok(pageInfo);
    }

    /**
     * 养殖散户列表
     *
     * @param areaList
     * @param farmId
     * @param linkman
     * @param phone
     * @param animalType
     * @param pageNum
     * @param pageSize
     * @return
     */
    @OpenMethod(key = "web.farms.farmsRetailList",
            paramNames = {"areaList", "farmId", "linkman", "phone", "animalType", "pageNum", "pageSize"},
            httpMethods = RequestMethod.GET)
    public Response<PageInfo<Farms>> farmsRetailList(List<Integer> areaList,
                                                     Long farmId,
                                                     String linkman,
                                                     String phone,
                                                     List<Integer> animalType,
                                                     Integer pageNum,
                                                     Integer pageSize) {

        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

        if (areaList.size() == 0 && StringUtil.isBlank(linkman) && StringUtil.isBlank(phone) && animalType.size() == 0) {
            LoginInfo loginInfo = UserUtil.getCurrentUser();
            Map map = new HashMap();
            map.put("farmType", FarmsEnum.Retail.getCode());
            map.put("userId", loginInfo.getId());
            PageInfo<Farms> pageInfo = farmsService.selectByUserIdPage(pageNum, pageSize, map);
            return Response.ok(pageInfo);
        }
        //基本搜索
        Farms farms = new Farms();
        farms.setFarmsType(FarmsEnum.Retail.getCode());

        Integer areaSize = areaList.size();
        switch (areaSize) {
            case 1:
                farms.setProvinceId(areaList.get(0));
                break;
            case 2:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                break;
            case 3:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                break;
            case 4:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                break;
            case 5:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                farms.setVillageId(areaList.get(4));
                break;
            default:
                break;


        }

        farms.setId(farmId);
        farms.setLinkman(linkman);
        farms.setPhone(phone);

        if (animalType != null) {
            Integer animalTypeLever = animalType.size();
            switch (animalTypeLever) {
                case 1:
                    farms.setAnimalOneType(animalType.get(0));
                    break;
                case 2:
                    farms.setAnimalTwoType(animalType.get(1));
                    break;
                case 3:
                    farms.setAnimalTwoType(animalType.get(1));
                default:
                    break;

            }
        }
        //farms.setDeleteFlag(0); //boss可以看到所有
        PageInfo<Farms> pageInfo = farmsService.getFarmsListByPage(pageNum, pageSize, farms);
        return Response.ok(pageInfo);
    }

    /**
     * 屠宰场列表
     *
     * @param areaList
     * @param farmId
     * @param linkman
     * @param phone
     * @param pageNum
     * @param pageSize
     * @return
     */

    //@GetMapping("/farmsButcherList")
    @OpenMethod(
            key = "web.farms.butcherList",
            paramNames = {"areaList", "farmId", "linkman", "phone", "animalType", "pageNum", "pageSize"},
            httpMethods = RequestMethod.GET
    )
    public Response<PageInfo<Farms>> farmsButcherList(List<Integer> areaList,
                                                      Long farmId,
                                                      String linkman,
                                                      String phone,
                                                      List<Integer> animalType,
                                                      Integer pageNum,
                                                      Integer pageSize) {

        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

        if (areaList.size() == 0 && StringUtil.isBlank(linkman) && StringUtil.isBlank(phone) && animalType.size() == 0) {
            LoginInfo loginInfo = UserUtil.getCurrentUser();
            Map map = new HashMap();
            map.put("farmType", FarmsEnum.Butcher.getCode());
            map.put("userId", loginInfo.getId());
            PageInfo<Farms> pageInfo = farmsService.selectByUserIdPage(pageNum, pageSize, map);
            return Response.ok(pageInfo);
        }
        //基本搜索
        Farms farms = new Farms();
        farms.setFarmsType(FarmsEnum.Butcher.getCode());

        Integer areaSize = areaList.size();
        switch (areaSize) {
            case 1:
                farms.setProvinceId(areaList.get(0));
                break;
            case 2:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                break;
            case 3:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                break;
            case 4:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                break;
            case 5:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                farms.setVillageId(areaList.get(4));
                break;
            default:
                break;


        }
        farms.setId(farmId);
        farms.setLinkman(linkman);
        farms.setPhone(phone);

        if (animalType != null) {
            Integer animalTypeLever = animalType.size();
            switch (animalTypeLever) {
                case 1:
                    farms.setAnimalOneType(animalType.get(0));
                    break;
                case 2:
                    farms.setAnimalTwoType(animalType.get(1));
                    break;
                case 3:
                    farms.setAnimalTwoType(animalType.get(1));
                default:
                    break;

            }
        }
        //  farms.setDeleteFlag(0); //boss可以看到所有


        PageInfo<Farms> pageInfo = farmsService.getFarmsListByPage(pageNum, pageSize, farms);


        return Response.ok(pageInfo);
    }

    /**
     * 根据ID查询详情养殖场/散户/屠宰场
     *
     * @param farmId
     * @return
     */
    @OpenMethod(key = "web.farms.selectByFarmId",
            paramNames = {"farmId"},
            httpMethods = RequestMethod.GET)
    public Response<FarmStockDto> selectByFarmId(Long farmId) {
        try {
            FarmStockDto farmsDto = new FarmStockDto();
            Farms farms = farmsService.selectByPrimaryKey(farmId);
            farmsDto.setFarms(farms);

            if (farms.getFarmsType() == FarmsEnum.Breed.getCode() || farms.getFarmsType() == FarmsEnum.Retail.getCode()) {
                FarmStock farmStock = farmStockService.selectByFarmID(farmId);
                farmsDto.setFarmStock(farmStock);
            }
            return Response.ok(farmsDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR, "请稍后再试!");
        }


    }

    /**
     * 搜索菜单根据区域查询Farm
     *
     * @param areaList
     * @param farmType
     * @return
     */
    @OpenMethod(
            key = "web.farms.farmListByArea",
            paramNames = {"areaList", "farmType"},
            httpMethods = RequestMethod.GET
    )
    public Response<List<Map>> farmsBreedList(List<Integer> areaList, Integer farmType
    ) {


        //基本搜索
        Farms farms = new Farms();
        farms.setFarmsType(farmType);
        Integer areaSize = areaList.size();
        switch (areaSize) {
            case 1:
                farms.setProvinceId(areaList.get(0));
                break;
            case 2:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                break;
            case 3:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                break;
            case 4:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                break;
            case 5:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                farms.setVillageId(areaList.get(4));
                break;
            default:
                break;


        }

        //  farms.setDeleteFlag(1); //boss可以看到所有

        List<Farms> pageInfo = farmsService.select(farms);
        List<Map> mapList = new ArrayList<>();
        for (Farms farms1 : pageInfo) {
            Map map = new HashMap();
            map.put("value", farms1.getId());
            map.put("label", farms1.getFarmsname());
            mapList.add(map);
        }


        return Response.ok(mapList);
    }

    /**
     * Farm停用用启用
     *
     * @param farmId
     * @param status
     * @return
     */
    @OpenMethod(
            key = "web.farms.updateFarm",
            paramNames = {"farmId", "status"},
            httpMethods = RequestMethod.POST
    )
    public Response<String> updateBreedFarm(Long farmId, Integer status) {


        Farms farms = new Farms();

        farms.setId(farmId);
        farms.setStatus(status);
        try {

            int result = farmsService.updateByPrimaryKeySelective(farms);

            if (status == 1) {

                return Response.ok("启用成功");
            } else {
                return Response.ok("停用成功");
            }

        } catch (Exception e) {
            log.error(e.getMessage());
            if (status == 1) {

                throw new OPServerException(HttpCode.DATA_UPDATE_FAIL, "启用失败请稍后再试");
            } else {
                throw new OPServerException(HttpCode.DATA_UPDATE_FAIL, "停用失败请稍后再试");
            }

        }

    }


    /**
     * 创建养殖场
     *
     * @param farms
     * @param mzUser
     * @param password
     * @return
     */
    @OpenMethod(key = "web.createFarm",
            paramNames = {"farms", "mzUser", "password"},
            httpMethods = RequestMethod.POST)
    @Transactional
    public Response<String> createFarm(Farms farms, MzUser mzUser, String password) {
        Long createID = UserUtil.getUserId();

        RegionAccount regionAccount = regionAccountService.selectByStreet(farms.getProvinceId(), farms.getCityId(), farms.getRegionId(), farms.getStreetId());
        if (regionAccount == null) {
            return Response.fail("此地区还没有开通请开通后再试！");
        }
        //创建养殖场
        try {
            //创建用户
            mzUser.setUserType(UserEnum.USERTYPE_CUS.getCode());//用户类型
            mzUser.setIsmaster(UserEnum.ismaster_true.getCode());//是否管理员
            mzUser.setMobile(farms.getPhone());
            mzUser.setCreatedId(createID);
            mzUser.setDeleteFlag(1);
            mzUser.setProvince(farms.getProvince());
            mzUser.setProvinceId(farms.getProvinceId());
            mzUser.setCity(farms.getCity());
            mzUser.setCityId(farms.getCityId());
            mzUser.setRegion(farms.getRegion());
            mzUser.setRegionId(farms.getRegionId());
            mzUser.setStreet(farms.getStreet());
            mzUser.setStreetId(farms.getStreetId());
            mzUser.setVillage(farms.getVillage());
            mzUser.setVillageId(farms.getVillageId());
            mzUser.setCreatedAt(new Date());
            Long userId = mzUserReadService.insert(mzUser);

            farms.setStatus(FarmsEnum.status_true.getCode());//状态
            farms.setIsRejectImm(FarmsEnum.isRejectImm_false.getCode());

            farms.setRegionAccountId(Math.toIntExact(regionAccount.getId()));//开户表Id
            farms.setCreatedId(createID);//创建人id
            farms.setCreatedAt(new Date());
            farms.setDeleteFlag(1);
            farms.setUser_id(userId);
            Long farmId = farmsService.insert(farms);



            FarmUser farmsUser = new FarmUser();
            farmsUser.setFarm_id(farmId);
            farmsUser.setUser_id(userId);
            farmsUser.setTag(1);
            farmsUserService.insert(farmsUser);



            //创建登录用户
            LoginUser loginUser = new LoginUser();
            loginUser.setUserId(userId);
            loginUser.setStatus(UserEnum.LOGIN_DISABLE.getCode());
            String salt = RandomUtil.generateLowerString(3);
            loginUser.setSalt(salt);
            loginUser.setPassword(MD5Util.encrypt(salt + password));
            loginUser.setUsername(farms.getPhone());
            loginUser.setErrorcount(0);
            loginUser.setLogincount(0);
            loginUser.setUnlocktime(new Date());
            ssoLoginService.insert(loginUser);
            //创建角色
            RoleAppUser roleAppUser = new RoleAppUser();
            roleAppUser.setAppRole(2009);
            roleAppUser.setAppRoleName("养殖场管理员");
            roleAppUser.setCreatedAt(new Date());
            roleAppUser.setStatus(1);
            roleAppUser.setCreatedId(createID);
            roleAppUser.setUserId(userId);
            roleAppUserService.insert(roleAppUser);
            //创建存栏
            FarmStock farmStock = new FarmStock();
            farmStock.setFarm_id(farmId);
            farmStock.setFarm_name(farms.getFarmsname());
            farmStock.setChange_stock(0);
            farmStock.setReal_stock(farms.getScale());
            farmStock.setInitial_stock(farms.getScale());
            farmStock.setUpdate_time(new Date());
            farmStockService.insert(farmStock);
            return Response.ok("创建成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Response.fail("创建失败请稍后再试");
        }
    }

    /**
     * 查询手机号
     *
     * @param phone
     * @return
     */
    @OpenMethod(key = "web.Phone.check",
            paramNames = {"phone"},
            httpMethods = RequestMethod.GET)
    public Response<String> phoneCheck(String phone) {
        MzUser mzUser = new MzUser();
        mzUser.setMobile(phone);
        MzUser mzUser1 = mzUserReadService.selectOne(mzUser);
        if (mzUser1 == null) {
            return Response.ok("此手机号可以使用");
        } else {
            return Response.fail("此手机号已经注册过");
        }
    }

    /**
     * 创建屠宰场
     *
     * @param farms
     * @return
     */
    @OpenMethod(key = "web.createButFarm",
            paramNames = {"farms", "mzUser", "password"},
            httpMethods = RequestMethod.POST)
    public Response createButFarm(Farms farms, MzUser mzUser, String password) {
        Long createID = UserUtil.getUserId();
        RegionAccount regionAccount = regionAccountService.selectByStreet(farms.getProvinceId(), farms.getCityId(), farms.getRegionId(), farms.getStreetId());
        if (regionAccount == null) {
            Response.fail("此地区还没有开通请开通后再试！");
        }
        try {



            mzUser.setUserType(UserEnum.USERTYPE_BUT.getCode());//用户类型
            mzUser.setIsmaster(UserEnum.ismaster_true.getCode());//是否管理员
            mzUser.setMobile(farms.getPhone());
            mzUser.setCreatedId(createID);
            mzUser.setDeleteFlag(1);
            mzUser.setProvince(farms.getProvince());
            mzUser.setProvinceId(farms.getProvinceId());
            mzUser.setCity(farms.getCity());
            mzUser.setCityId(farms.getCityId());
            mzUser.setRegion(farms.getRegion());
            mzUser.setRegionId(farms.getRegionId());
            mzUser.setStreet(farms.getStreet());
            mzUser.setStreetId(farms.getStreetId());
            mzUser.setVillage(farms.getVillage());
            mzUser.setVillageId(farms.getVillageId());
            mzUser.setCreatedAt(new Date());
            Long userId = mzUserReadService.insert(mzUser);

            farms.setStatus(1);
            farms.setFarmsType(FarmsEnum.Butcher.getCode());
            farms.setRegionAccountId(Math.toIntExact(regionAccount.getId()));//开户表Id
            farms.setCreatedId(createID);//创建人id
            farms.setCreatedAt(new Date());
            farms.setDeleteFlag(1);
            farms.setUser_id(userId);
            Long farmId = farmsService.insert(farms);



            FarmUser farmsUser = new FarmUser();
            farmsUser.setFarm_id(farmId);
            farmsUser.setUser_id(userId);
            farmsUser.setTag(1);
            farmsUserService.insert(farmsUser);

            //创建登录用户
            LoginUser loginUser = new LoginUser();
            loginUser.setUserId(userId);
            loginUser.setStatus(UserEnum.LOGIN_DISABLE.getCode());
            String salt = RandomUtil.generateLowerString(3);
            loginUser.setSalt(salt);
            loginUser.setPassword(MD5Util.encrypt(salt + password));
            loginUser.setUsername(farms.getPhone());
            loginUser.setErrorcount(0);
            loginUser.setLogincount(0);
            loginUser.setUnlocktime(new Date());
            ssoLoginService.insert(loginUser);
            //创建角色
            RoleAppUser roleAppUser = new RoleAppUser();
            roleAppUser.setAppRole(2011);
            roleAppUser.setAppRoleName("屠宰场管理员");
            roleAppUser.setCreatedAt(new Date());
            roleAppUser.setStatus(1);
            roleAppUser.setCreatedId(createID);
            roleAppUser.setUserId(userId);
            roleAppUserService.insert(roleAppUser);
            return Response.ok("创建成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Response.ok("创建失败请稍后再试");
        }

    }


    /**
     * 更新养殖场
     *
     * @param farms
     * @return
     */
    @OpenMethod(key = "web.farm.updateFarm",
            paramNames = {"farms"},
            httpMethods = RequestMethod.POST)
    public Response<String> updateFarm(Farms farms) {
        try {
            if (farms.getId() == null) {
                Response.fail(HttpCode.FARM_ID_IS_NOT_EXIST, "养殖场获取失败");
            }
            farmsService.updateByPrimaryKeySelective(farms);
            return Response.ok("更新成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Response.fail(HttpCode.DATA_UPDATE_FAIL, "更新失败");
        }

    }

    @OpenMethod(key = "web.farm.create.area",
            paramNames = {"userId", "level"},
            httpMethods = RequestMethod.GET)
    public Response getProvince(Long userId, Integer level) {
        List<Object> list = orgUserAreaService.queryLocationArea(userId);
        List<SysLabelModel> sysLabelModels = new ArrayList<>();
        switch (level) {
            case 1:
                sysLabelModels = sysLabelModels(list);
                break;
            case 2:
                for (int i = 0; i < list.size(); i++) {
                    Object provinceObject = list.get(i);
                    UserAreaModel userAreaModel = (UserAreaModel) provinceObject;
                    List<Object> cityObjectList = userAreaModel.getChildren();
                    sysLabelModels = sysLabelModels(cityObjectList);

                }
                break;
            case 3:
                for (int i = 0; i < list.size(); i++) {
                    Object provinceObject = list.get(i);
                    UserAreaModel userAreaModel = (UserAreaModel) provinceObject;
                    List<Object> cityObjectList = userAreaModel.getChildren();
                    for (int j = 0; j < cityObjectList.size(); j++) {
                        Object cityObject = cityObjectList.get(j);
                        UserAreaModel cityModel = (UserAreaModel) cityObject;
                        List<Object> regionObjectList = cityModel.getChildren();
                        sysLabelModels = sysLabelModels(regionObjectList);
                    }

                }
                break;

            case 4:
                for (int i = 0; i < list.size(); i++) {
                    Object provinceObject = list.get(i);
                    UserAreaModel userAreaModel = (UserAreaModel) provinceObject;
                    List<Object> cityObjectList = userAreaModel.getChildren();
                    for (int j = 0; j < cityObjectList.size(); j++) {
                        Object cityObject = cityObjectList.get(j);
                        UserAreaModel cityModel = (UserAreaModel) cityObject;
                        List<Object> regionObjectList = cityModel.getChildren();
                        for (int k = 0; k < regionObjectList.size(); k++) {
                            Object streetObject = regionObjectList.get(k);
                            UserAreaModel streetModel = (UserAreaModel) streetObject;
                            sysLabelModels = sysLabelModels(streetModel.getChildren());
                        }

                    }

                }
                break;
            case 5:
                for (int i = 0; i < list.size(); i++) {
                    Object provinceObject = list.get(i);
                    UserAreaModel userAreaModel = (UserAreaModel) provinceObject;
                    List<Object> cityObjectList = userAreaModel.getChildren();
                    for (int j = 0; j < cityObjectList.size(); j++) {
                        Object cityObject = cityObjectList.get(j);
                        UserAreaModel cityModel = (UserAreaModel) cityObject;
                        List<Object> regionObjectList = cityModel.getChildren();
                        for (int k = 0; k < regionObjectList.size(); k++) {
                            Object streetObject = regionObjectList.get(k);
                            UserAreaModel streetModel = (UserAreaModel) streetObject;
                            List<Object> villageObjectList = streetModel.getChildren();
                            for (int m = 0; m < villageObjectList.size(); m++) {
                                Object villageObject = villageObjectList.get(m);
                                SysLabelModel sysLabelModel = (SysLabelModel) villageObject;
                                sysLabelModels.add(sysLabelModel);
                            }
                        }

                    }

                }
                break;
        }
        return Response.ok(sysLabelModels);
    }


    public List<SysLabelModel> sysLabelModels(List<Object> objectList) {
        List<SysLabelModel> sysLabelModels = new ArrayList<>();
        for (int i = 0; i < objectList.size(); i++) {
            Object object = objectList.get(i);
            UserAreaModel userAreaModel = (UserAreaModel) object;
            SysLabelModel sysLabelModel = new SysLabelModel();
            sysLabelModel.setValue(userAreaModel.getValue());
            sysLabelModel.setLabel(userAreaModel.getLabel());
            sysLabelModels.add(sysLabelModel);
        }

        return sysLabelModels;
    }

    /**
     * 下拉查询时，根据养殖类型取值
     *
     * @param farmType
     * @return
     */

    //@GetMapping("/querySelectFarmList")
    @OpenMethod(
            key = "web.farms.selectFarmList",
            paramNames = {"farmType"},
            httpMethods = RequestMethod.GET
    )
    public Response<List<Farms>> querySelectFarmList(Integer farmType) {
        if (farmType == null || farmType == 0) {
            return Response.fail(HttpCode.BAD_REQUEST, "企业类型不能为空");
        }
        return Response.ok(farmsService.querySelectFarmList(farmType, ""));
    }



    /**
     * 牛档案政府端 -- 养殖场列表 (养牛的养殖场)
     */
    @OpenMethod(key = "web.farms.bull.selectFarmListByAnimalType",
                            paramNames = {},
                            httpMethods = RequestMethod.GET)
    public Response<List<Map<String,Object>>> selectFarmListByAnimalType(){


        if (farmsService == null) {
            return Response.fail("farmsService 服务为空,请检查后重试");
        }

        List<Map<String,Object>> list = Lists.newArrayList();

        Integer breedSecondType = AnimalTypeEnum.BULL.getCode();

        Long userId = UserUtil.getUserId();
        List<Farms> farmsList = farmsService.selectFarmListByAnimalType(userId,breedSecondType);

        for (Farms farms : farmsList) {
            Map<String,Object> result = Maps.newHashMap();
            result.put("value",farms.getId());
            result.put("label",farms.getFarmsname());
            list.add(result);
        }

        return Response.ok(list);
    }



}
